const app = getApp()
const i18n = require('../../../utils/i18n/index')

Page({
  data: {
    t: {},
    showPhoneAuth: false,
    openid: ''
  },

  onShow() {
    // 更新导航栏标题
    wx.setNavigationBarTitle({
        title: i18n.t('common.appname')
    });
  },

  onLoad() {
    this.setI18nData()
  },

  setI18nData() {
    this.setData({
      t: {
        title: i18n.t('mine.loginTitle'),
        loginWithPhone: i18n.t('mine.loginWithPhone'),
        phoneAuthTitle: i18n.t('mine.phoneAuthTitle') || '手机号授权',
        phoneAuthDesc: i18n.t('mine.phoneAuthDesc') || '为保障账号安全与服务体验，请授权获取您的微信手机号，仅需一次操作。',
        phoneAuthBtn: i18n.t('mine.phoneAuthBtn') || '授权手机号',
        cancel: i18n.t('common.cancel') || '取消',
        loginSuccess: i18n.t('mine.login.success') || '登录成功',
        loginFailed: i18n.t('mine.login.failed') || '登录失败',
        authFailed: i18n.t('mine.login.authFailed') || '授权失败',
        loading: i18n.t('common.loading') || '加载中...'
      }
    })
  },

  // 登录处理
  onLogin() {
    wx.login({
      success: res => {
        if (res.code) {
          wx.request({
            url: app.globalData.baseUrl + 'auth-session',
            method: 'GET',
            data: { code: res.code },
            success: loginRes => {
              if (loginRes.data.code === 0) {
                const token = loginRes.data.token
                console.log("token", token)
                if (token) {
                  // 直接登录
                  wx.setStorageSync('token', token)
                  app.globalData.token = token
                  app.globalData.isLogin = true
                  wx.showToast({ title: this.data.t.loginSuccess, icon: 'success' })
                  setTimeout(() => wx.navigateBack(), 1500)
                } else {
                  // 需要手机号
                  this.setData({
                    showPhoneAuth: true,
                    openid: loginRes.data.openid
                  })
                }
              } else {
                wx.showToast({ title: this.data.t.loginFailed, icon: 'none' })
              }
            },
            fail: () => {
              wx.showToast({ title: this.data.t.loginFailed, icon: 'none' })
            }
          })
        }
      }
    })
  },

  // 获取手机号
  onGetPhoneNumber(e) {
    if (e.detail.errMsg !== 'getPhoneNumber:ok') {
      wx.showToast({ title: this.data.t.authFailed, icon: 'none' })
      return
    }
    wx.showLoading({ title: this.data.t.loading })
    wx.request({
      url: app.globalData.baseUrl + 'phone-login',
      method: 'POST',
      data: {
        openid: this.data.openid,
        iv: e.detail.iv,
        phone_data: e.detail.encryptedData
      },
      success: res => {
        if (res.data.code === 0) {
          const token = res.data.token || (res.data.data && res.data.data.token)
          wx.setStorageSync('token', token)
          app.globalData.token = token
          app.globalData.isLogin = true
          wx.showToast({ title: this.data.t.loginSuccess, icon: 'success' })
          setTimeout(() => wx.navigateBack(), 1500)
        } else {
          wx.showToast({ title: this.data.t.loginFailed, icon: 'none' })
        }
      },
      complete: () => {
        wx.hideLoading()
        this.setData({ showPhoneAuth: false })
      }
    })
  },

  // 关闭手机号弹窗
  onCancelPhoneAuth() {
    this.setData({ showPhoneAuth: false })
  }
}) 